package com.syf.kit.core.ds.impl;

import com.syf.kit.cont.RdsDSCont;
import com.syf.kit.enums.DataSourceType;
import com.syf.kit.enums.RDSType;

import javax.sql.DataSource;
import java.util.Map;
import java.util.Set;

public class SyfMultipleDataSource implements SyfDataSource {
    private final RDSType rdsType;

    private final DataSource defaultDs;

    private final Map<String, DataSource> dsMap;

    public SyfMultipleDataSource(RDSType rdsType, DataSource defaultDs, Map<String, DataSource> dsMap) {
        this.rdsType = rdsType;
        this.defaultDs = defaultDs;
        this.dsMap = dsMap;
        dsMap.put(RdsDSCont.DEFAULT_DS_NAME, defaultDs);
    }

    @Override
    public RDSType getRDSType() {
        return rdsType;
    }

    @Override
    public DataSourceType getDsType() {
        return DataSourceType.MULTIPLE;
    }

    @Override
    public DataSource getDefaultDataSource() {
        return defaultDs;
    }

    @Override
    public DataSource getDataSourceByName(String name) {
        if (!dsMap.containsKey(name)) {
            throw new RuntimeException("数据源:" + name + "不存在，请仔细核对");
        }

        return dsMap.get(name);
    }

    @Override
    public Set<String> getDataSourceNameSet() {
        return dsMap.keySet();
    }
}
